/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog;

import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogEditDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogEditIdDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogPreviewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogRemoveDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogUpdateDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.exceptions.BlogNotFoundException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.exceptions.BlogTitleEmptyException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.exceptions.NotBlogOwnerException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.dashboard.UserBlogTitleDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserSessionId;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.NotLoggedInException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogNotFoundEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogSuccessfulRemovedEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogSuccessfulUpdatedEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogSuccessulAddedEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogTitleEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.NotBlogOwnerEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.ReceiveBlogDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.ReceiveBlogEditDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.ReceiveListBlogTitleDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.ReceiveSetBlogPreviewDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.events.NotLoggedInEvent;
import com.googlecode.hmvc4gwt.framework.client.rpc.ASessionAsyncCallback;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.event.IEventListener;

/**
 * 
 * @author jens.meiss
 */
public class BlogAsynCallback<T, E extends IEvent> extends
		ASessionAsyncCallback<T, E> {

	private static IBlogRemoteServiceAsync remoteService;

	public static void addBlog(BlogNewDTO blogNewDTO,
			IEventListener eventListener) {

		BlogAsynCallback<Void, IEvent> callback = new BlogAsynCallback<Void, IEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new BlogSuccessulAddedEvent());

		blogNewDTO.setSessionId(getSessionId());
		
		getRemoteService().addBlog(blogNewDTO, callback);
	}

	public static void update(BlogUpdateDTO blogUpdateDTO, IEventListener eventListener) {
		
		BlogAsynCallback<Void, BlogSuccessfulUpdatedEvent> callback = new BlogAsynCallback<Void, BlogSuccessfulUpdatedEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new BlogSuccessfulUpdatedEvent());
		
		blogUpdateDTO.setSessionId(getSessionId());
		
		getRemoteService().update(blogUpdateDTO, callback);
		
	}
	
	public static void getEditBlogDTO(BlogEditIdDTO blogEditIdDTO, IEventListener eventListener) {
		
		BlogAsynCallback<BlogEditDTO, ReceiveBlogEditDTOEvent> callback = new BlogAsynCallback<BlogEditDTO, ReceiveBlogEditDTOEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new ReceiveBlogEditDTOEvent());
		
		blogEditIdDTO.setSessionId(getSessionId());
		
		getRemoteService().getEditBlogDTO(blogEditIdDTO, callback);
	}
	
	public static void getBlog(Integer id, IEventListener eventListener) {

		BlogAsynCallback<BlogDTO, ReceiveBlogDTOEvent> callback = new BlogAsynCallback<BlogDTO, ReceiveBlogDTOEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new ReceiveBlogDTOEvent());

		getRemoteService().getBlog(id, callback);

	}

	public static void getBlog(String name, IEventListener eventListener) {

		BlogAsynCallback<BlogDTO, ReceiveBlogDTOEvent> callback = new BlogAsynCallback<BlogDTO, ReceiveBlogDTOEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new ReceiveBlogDTOEvent());

		getRemoteService().getBlog(name, callback);

	}

	public static void getListBlogTitleDTOEvent(IEventListener eventListener) {

		BlogAsynCallback<List<UserBlogTitleDTO>, ReceiveListBlogTitleDTOEvent> callback = new BlogAsynCallback<List<UserBlogTitleDTO>, ReceiveListBlogTitleDTOEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new ReceiveListBlogTitleDTOEvent());

		UserSessionId userSessionId = new UserSessionId();
		userSessionId.setSessionId(getSessionId());
		
		getRemoteService().getListBlogTitleDTO(userSessionId, callback);
	}

	private static IBlogRemoteServiceAsync getRemoteService() {

		if (remoteService == null) {
			remoteService = (IBlogRemoteServiceAsync) GWT.create(IBlogRemoteService.class);
		}

		return remoteService;

	}

	public static void getSetBlogPreview(IEventListener eventListener) {

		BlogAsynCallback<Set<BlogPreviewDTO>, ReceiveSetBlogPreviewDTOEvent> callback = new BlogAsynCallback<Set<BlogPreviewDTO>, ReceiveSetBlogPreviewDTOEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new ReceiveSetBlogPreviewDTOEvent());

		getRemoteService().getSetBlogPreview(callback);

	}

	public static void removeBlog(BlogRemoveDTO blogRemoveDTO,
			IEventListener eventListener) {

		BlogAsynCallback<Void, BlogSuccessfulRemovedEvent> callback = new BlogAsynCallback<Void, BlogSuccessfulRemovedEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new BlogSuccessfulRemovedEvent());

		blogRemoveDTO.setSessionId(getSessionId());
		
		getRemoteService().removeBlog(blogRemoveDTO, callback);

	}

	@Override
	public void onFailure(Throwable caught) {

		if (caught.getClass() == BlogTitleEmptyException.class) {

			getEventListener().handleEvent(new BlogTitleEmptyEvent());

		} else if (caught.getClass() == NotLoggedInException.class) {

			getEventListener().handleEvent(new NotLoggedInEvent());

		} else if (caught.getClass() == BlogNotFoundException.class) {

			getEventListener().handleEvent(new BlogNotFoundEvent());

		} else if (caught.getClass() == NotBlogOwnerException.class) {

			getEventListener().handleEvent(new NotBlogOwnerEvent());
		}
	}
}
